package com.z.sys.controller;

import com.github.pagehelper.PageInfo;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.DeptUserRole;
import com.z.sys.entity.Role;
import com.z.sys.service.IDeptUserRoleService;
import com.z.sys.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 人员角色授权Controller
 *
 * @author z
 * @date 2023-03-01
 */
@RestController
@RequestMapping("/system/deptUserRole")
public class DeptUserRoleController extends BaseController {

    private String prefix = "system/deptUserRole";

    @Autowired
    private IDeptUserRoleService deptUserRoleService;
    @Autowired
    private IRoleService roleService;

    @GetMapping("/main")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }
    @GetMapping("/main_v2")
    public ModelAndView main_v2() {
        return jumpPage(prefix + "/main_v2");
    }

    @GetMapping("/main_unit_admin_set")
    public ModelAndView main_unit_admin_set() {
        return jumpPage(prefix + "/main_unit_admin_set");
    }

    /**
     * 查询人员角色授权列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result list(@ModelAttribute DeptUserRole deptUserRole, PageVo pageVo) {
        PageInfo<DeptUserRole> pageInfo = deptUserRoleService.selectDeptUserRolePage(deptUserRole, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 查询机构可授权角色
     */
    @ResponseBody
    @GetMapping("/selectRoleByDeptId")
    public Result selectRoleByDeptId(Long deptId) {
        if(null == deptId){
            return Result.table(new ArrayList());
        }
        List<Role> list = roleService.selectRoleByDeptId(deptId);

        return Result.table(list);
    }

    /**
     * 查询机构可授权人事角色
     */
    @ResponseBody
    @GetMapping("/selectUnitAdminByDeptId")
    public Result selectUnitAdminByDeptId(Long deptId) {
        if(null == deptId){
            return Result.table(new ArrayList());
        }
        List<Role> list = roleService.selectUnitAdminByDeptId(deptId);

        return Result.table(list);
    }

    /**
     * 新增人员角色授权
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增人员角色授权
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody DeptUserRole deptUserRole) {
        if(StringUtils.isEmpty(deptUserRole.getUserId()) || StringUtils.isEmpty(deptUserRole.getRoleId()) || deptUserRole.getDeptId() == null){
            return Result.error("参数有误");
        }
        return Result.decide(deptUserRoleService.save(deptUserRole));
    }

    /**
     * 修改人员角色授权
     */
    @GetMapping("/edit")
    public ModelAndView edit(Long id, ModelMap map) {
        DeptUserRole deptUserRole =deptUserRoleService.getById(id);
        map.put("deptUserRole", deptUserRole);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改人员角色授权
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody DeptUserRole deptUserRole) {
        return Result.decide(deptUserRoleService.updateById(deptUserRole));
    }

    /**
     * 删除人员角色授权
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return Result.decide(deptUserRoleService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除人员角色授权
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(deptUserRoleService.removeById(id));
    }
}
